package com.lin.utils.treeuntil;

import com.lin.dataobject.Features;
import com.lin.utils.treenode.FeaturesTreeNode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/2.
 */
public class FeatureTreeUntil {

    public List<FeaturesTreeNode> fillTree(List<Features> institutions){
        List<FeaturesTreeNode> list=new ArrayList<FeaturesTreeNode>();
        Map map=new HashMap<String, FeaturesTreeNode>();
        try {
            for(Features institution:institutions){
                FeaturesTreeNode node=new FeaturesTreeNode();
                node.setId(institution.getId());
                node.setText(institution.getName());
                FeaturesTreeNode.Attributes attributes=node.new Attributes();
                attributes.setUrl(institution.getUrl());
                node.setAttributes(attributes);
                if(institution.getPid()!=0){
                    node.setPid(institution.getPid());
                }
                map.put(institution.getId(), node);
            }

            for(Features institution:institutions){
                if(institution.getPid()!=0){
                    if(institution.getPid()==0){

                        list.add((FeaturesTreeNode)map.get(institution.getId()));
                    }else{
                        Integer pId=institution.getPid();
                        FeaturesTreeNode pNode=(FeaturesTreeNode)map.get(pId);
                        FeaturesTreeNode cNode=(FeaturesTreeNode)map.get(institution.getId());
                        pNode.addChild(cNode);
                    }
                }else {
                    list.add((FeaturesTreeNode)map.get(institution.getId()));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
